package com.ruoyi.web.controller.pc.anon;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.blogs.domain.BsBrowseHistory;
import com.ruoyi.blogs.domain.BsFaq;
import com.ruoyi.blogs.domain.vo.BsAnswerVo;
import com.ruoyi.blogs.domain.vo.BsFaqVo;
import com.ruoyi.blogs.service.IBsAnswerService;
import com.ruoyi.blogs.service.IBsBrowseHistoryService;
import com.ruoyi.blogs.service.IBsFaqService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "问答")
@RestController
@RequestMapping("/anon/faq")
public class AnonFaqController extends BaseController{
    
    @Autowired
    private IBsFaqService bsFaqService;

    @Autowired
    private IBsAnswerService bsAnswerService;

    @Autowired
    private IBsBrowseHistoryService bsBrowseHistoryService;

    /**
     * 查询问答管理列表
     */
    @ApiOperation("获取问答列表")
    @GetMapping("/list")
    public TableDataInfo list(BsFaqVo bsFaq)
    {
        startPage();
        bsFaq.setVisible(0);
        List<BsFaqVo> list = bsFaqService.selectBsFaqVoList(bsFaq);
        return getDataTable(list);
    }

    /**
     * 获取问答详情
     */
    @ApiOperation("获取问答详情")
    @Transactional
    @GetMapping
    public R<BsFaqVo> getInfo(BsFaq bsFaq)
    {
        bsFaqService.updateBsFaqViewById(bsFaq);
        try {
            Map<String, Object> map = new HashMap();
            map.put("userId", SecurityUtils.getUserId());
            bsFaq.setParams(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 查询问答详情
        BsFaqVo bsFaqVo = bsFaqService.selectBsFaqVoById(bsFaq);
        BsAnswerVo bsAnswerVo = new BsAnswerVo();
        try {
            // 添加历史记录
            BsBrowseHistory bsBrowseHistory = new BsBrowseHistory();
            bsBrowseHistory.setConnectId(bsFaq.getId());
            bsBrowseHistory.setType(1);
            bsBrowseHistory.setTitle(bsFaqVo.getTitle());
            bsBrowseHistoryService.insertBsBrowseHistory(bsBrowseHistory);
            Map<String, Object> map = new HashMap();
            map.put("userId", SecurityUtils.getUserId());
            bsAnswerVo.setParams(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 查询查询问答下的回答
        startPage();
        List<BsAnswerVo> list = bsAnswerService.selectBsAnswerVoList(bsAnswerVo);
        TableDataInfo table = getDataTable(list);
        bsFaqVo.setBsAnswers((List<BsAnswerVo>) table.getRows());
        bsFaqVo.setAnswerNum(table.getTotal());
        return R.ok(bsFaqVo);
    }
}
